home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / serverlib / sync / signalLatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  4.1 KB  |  183 lines

  1. /*
  2.  *   $RCSfile: signalLatch.c,v $  
  3.  *   $Revision: 1.1.1.1 $  
  4.  *   $Date: 1996/05/04 21:55:59 $      
  5.  */ 
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37.  
  38. #include "sysdefs.h"
  39. #include "ess.h"
  40. #include "checking.h"
  41. #include "trace.h"
  42. #include "error.h"
  43. #include "list.h"
  44. #include "tid.h"
  45. #include "io.h"
  46. #include "lock.h"
  47. #include "object.h"
  48. #include "msgdefs.h"
  49. #include "thread.h"
  50. #include "latch.h"
  51. #include "threadstate.h"
  52. #include "thread_globals.h"
  53.  
  54.  
  55.  void
  56. signalLatch (
  57.  
  58.     register LATCH    *latch 
  59. )
  60. {
  61.  
  62.     register TCB    *tcb;
  63.  
  64.  
  65.     TRACE(TR_LATCH, TR_LEVEL_1);
  66.  
  67.     /*
  68.      *    check the semaphore magic number
  69.      */
  70.     CHECK_LATCH_MAGIC(latch);
  71.  
  72.     /*
  73.      *    check to see what type of latch is held
  74.      */
  75.     if (latch->shareCount != 0)    {
  76.  
  77.         /*
  78.          *    decrement the share count
  79.          */
  80.         latch->shareCount--;
  81.  
  82.         /*
  83.          *    check to see if the count is sane
  84.          */
  85.         SM_ASSERT(LEVEL_3, (latch->shareCount >= 0) && (latch->exclusiveFlag == 0));
  86.  
  87.     } else {
  88.  
  89.         /*
  90.          *    check to see that the exclusive flag is set
  91.          */
  92.         SM_ASSERT(LEVEL_3, latch->exclusiveFlag != 0);
  93.  
  94.         /*
  95.          *    release the exclusive latch
  96.          */
  97.         latch->exclusiveFlag = 0;
  98.     }
  99.  
  100.     /*
  101.      *    now we must grant outstanding latch requests
  102.      */
  103.     while (LIST_NOT_EMPTY( &(latch->waitList) ))    {
  104.  
  105.         /*
  106.          *    get a pointer to the next waiter
  107.          */
  108.         tcb = (TCB *) FIRST_LIST_ELEMENT( &(latch->waitList) );
  109.         CHECK_TCB_MAGIC(tcb);
  110.  
  111.         /*
  112.          *    check for the type of latch requested
  113.          */
  114.         if (tcb->state == THREAD_SH_LATCH_WAIT)    {
  115.  
  116.             TRPRINT(TR_LATCH, TR_LEVEL_2, ("thread:%d in share wait", tcb->id));
  117.  
  118.             /*
  119.              *    check to see if there is an outstanding exclusive latch
  120.              */
  121.             if (latch->exclusiveFlag == 0)    {
  122.  
  123.                 TRPRINT(TR_LATCH, TR_LEVEL_2, ("granting share latch thread:%d", tcb->id));
  124.  
  125.                 /*
  126.                  *    grant request
  127.                  */
  128.                 listDeq( &(latch->waitList) );
  129.                 latch->shareCount++;
  130.                 listEnq( &ReadyList, &(tcb->controlList) );
  131.  
  132.             } else {
  133.  
  134.                 /*
  135.                  *    cannot grant any more requests
  136.                  */
  137.                 TRPRINT(TR_LATCH, TR_LEVEL_2, ("cannot grant more requests"));
  138.                 break;
  139.             }
  140.  
  141.         } else if (tcb->state == THREAD_EX_LATCH_WAIT)    {
  142.  
  143.             TRPRINT(TR_LATCH, TR_LEVEL_2, ("thread:%d in exclusive wait", tcb->id));
  144.  
  145.             /*
  146.              *    check to see if there are any share or
  147.              *    exclusive latches granted
  148.              */
  149.             if ((latch->exclusiveFlag == 0) && (latch->shareCount == 0))    {
  150.  
  151.                 TRPRINT(TR_LATCH, TR_LEVEL_2, ("granting exclusive latch to:%d", tcb->id));
  152.  
  153.                 /*
  154.                  *    grant request
  155.                  */
  156.                 listDeq( &(latch->waitList) );
  157.                 latch->exclusiveFlag = 1;
  158.                 listEnq( &ReadyList, &(tcb->controlList) );
  159.  
  160.                 /*
  161.                  *    cannot grant any more requests
  162.                  */
  163.                 break;
  164.  
  165.             } else {
  166.  
  167.                 /*
  168.                  *    cannot grant any more requests
  169.                  */
  170.                 TRPRINT(TR_LATCH, TR_LEVEL_2, ("cannot grant more requests"));
  171.                 break;
  172.             }
  173.  
  174.         } else {
  175.  
  176.             /*
  177.              *    this cannot happen
  178.              */
  179.             SM_ERROR(TYPE_FATAL, esmINTERNAL);
  180.         }
  181.     }
  182. }
  183.